package com.tungse.vthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author : Tungse
 * @Date: 2025/8/14 17:05
 * @Description:
 */
public class EchoServer {
    public static void main(String[] args) throws IOException {
        int portNumber = 8090;
        ServerSocket serverSocket = new ServerSocket(portNumber);
        while (true) {
            Socket socket = serverSocket.accept();
            Thread.ofVirtual().start(() -> {
                try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
                ) {
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        String threadInfo = "Thread: " + Thread.currentThread().toString();
                        out.println(threadInfo + " : " +inputLine);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}